<?php
require('config.php');
$time2=date('Y-m');
$destination_folder="../Upload/refund/".$time2.'/'; //上传文件路径 
$destination_folder2="Upload/refund/".$time2.'/'; //存数据库路径 

$sum=0;
$src=array();
if(isset($_POST["file1"])) $sum=1;
if(isset($_POST["file2"])) $sum=2;
if(isset($_POST["file3"])) $sum=3;
if(isset($_POST["file4"])) $sum=4;
for($i=0;$i<$sum;$i++)
{
    $base64 = $_POST["file".$i]; // 得到参数
    $img = base64_decode($base64); // 将格式为base64的字符串解码
    $path = md5(uniqid(rand())).".jpg"; // 产生随机唯一的名字作为文件名
    $src_lin=$destination_folder.$path;
    file_put_contents($src_lin, $img); // 将图片保存到相应位置
    $src[$i]=$destination_folder2.$path;
}
for($i=0;$i<4;$i++)
{
    if($i<$sum) ;
    else $src[$i]='';
}
        $src1=$src[0];
        $src2=$src[1];
        $src3=$src[2];
        $src4=$src[3];
        $user_id=$_REQUEST['user_id'];
        $orderId=$_REQUEST['orderId'];
        $goodsId=$_REQUEST['goodsId'];
        $shopId=$_REQUEST['shopId'];
        $goodsScore=$_REQUEST['goodsScore'];
        $niming=$_REQUEST['niming'];
        $text=$_REQUEST['text'];
        // $src1=$_REQUEST['src1'];
        // $src2=$_REQUEST['src2'];
        // $src3=$_REQUEST['src3'];
        // $src4=$_REQUEST['src4'];
        $pan=$_REQUEST['pan'];

        file_put_contents("tsxx_sd_for_ios.txt", "\r\n接口调用时间：".date('Y-m-d H:i:s')."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\n收到".$sum."张图片\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\n分别为：".json_encode($src)."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nuser_id:".$user_id."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\norderId:".$orderId."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\ngoodsId:".$goodsId."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nshopId:".$shopId."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\ngoodsScore:".$goodsScore."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\nniming:".$niming."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\ntext:".$text."\r\n", FILE_APPEND);
        file_put_contents("tsxx_sd_for_ios.txt", "\r\npan:".$pan."\r\n", FILE_APPEND);

        $time=date('Y-m-d H:i:s');
		$sql="select * from `".$oto."_share` where `userId`='".$user_id."' and orderId='{$orderId}' and goodsId='{$goodsId}' and shopId='{$shopId}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$share_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
        $sql="select * from `".$oto."_goods_appraises` where `userId`='".$user_id."' and orderId='{$orderId}' and goodsId='{$goodsId}' and shopId='{$shopId}'";
        $result=$db->query($sql);
        while($row=$result->fetch_assoc()){
            $appraises_info[]=$row;//将取得的所有数据赋值给person_info数组
        }
        if($pan=='putong')
        {
            if($src1=='' || is_null($src1))
            {
                $sql="INSERT INTO ".$oto."_goods_appraises (`userId`,`orderId`,`goodsId`,`shopId`,`content`,`createTime`,`anonymity`,`isShow`,`goodsScore`) VALUES ('$user_id','$orderId','$goodsId','$shopId','$text','$time','$niming','1','$goodsScore')";
                $result=$db->query($sql);
                if($result)
                {
                    $data[0]['pan']=1;
                }
                else
                {
                    $data[0]['pan']=0;
                }
            }
            else
            {
                $sql="INSERT INTO ".$oto."_goods_appraises (`userId`,`orderId`,`goodsId`,`shopId`,`content`,`createTime`,`anonymity`,`isShow`,`goodsScore`) VALUES ('$user_id','$orderId','$goodsId','$shopId','$text','$time','$niming','1','$goodsScore')";
                $result=$db->query($sql);
                $sql="INSERT INTO ".$oto."_share (`userId`,`orderId`,`goodsId`,`shopId`,`star`,`shareTitle`,`shareContent`,`shareImg1`,`shareImg2`,`shareImg3`,`shareImg4`,`shareTime`,`anonymity`,`isShow`) VALUES ('$user_id','$orderId','$goodsId','$shopId','$goodsScore','$text','$text','$src1','$src2','$src3','$src4','$time','$niming','0')";
                $result2=$db->query($sql);
                if($result)
                {
                    $data[0]['pan']=1;
                }
                else
                {
                    $data[0]['pan']=0;
                }
            }
        }
        else if($pan=='zhuijia')
        {
            if(!isset($share_info))
            {
                $star=$appraises_info[0]['goodsScore'];
                $shareTitle=$appraises_info[0]['content'];
                $anonymity=$appraises_info[0]['anonymity'];
                $sql="INSERT INTO ".$oto."_share (`userId`,`orderId`,`goodsId`,`shopId`,`star`,`shareTitle`,`shareContent`,`shareImg1`,`shareImg2`,`shareImg3`,`shareImg4`,`shareTime`,`anonymity`,`isShow`) VALUES ('$user_id','$orderId','$goodsId','$shopId','$star','$shareTitle','$shareTitle','$src1','$src2','$src3','$src4','$time','$anonymity','0')";
                $result=$db->query($sql);
                if($result)
                {
                    $data[0]['pan']=1;
                }
                else
                {
                    $data[0]['pan']=0;
                }
                // $sql="update ".$oto."_order set isAppraises='1' where orderId='{$orderId}'";
                // $result=$db->query($sql);
            }
            else
            {
                $sql="update ".$oto."_share set shareImg1='{$src1}',shareImg2='{$src2}',shareImg3='{$src3}',shareImg4='{$src4}' where userId='{$user_id}' and orderId='{$orderId}' and goodsId='{$goodsId}' and shopId='{$shopId}'";
                $result=$db->query($sql);
                if($result)
                {
                    $data[0]['pan']=1;
                }
                else
                {
                    $data[0]['pan']=0;
                }
                $sql="update ".$oto."_goods_appraises set goodsScore='{$goodsScore}' where orderId='{$orderId}' and goodsId='{$goodsId}' and shopId='{$shopId}' and userId='{$user_id}'";
                $result=$db->query($sql);
            }
        }
		echo json_encode($data);
?>